package com.longshen.dubbo.api.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.logging.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.longshen.dubbo.api.sequence.SequenceService;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class TradeLogInterceptor  extends HandlerInterceptorAdapter {
	
	@Autowired
	private SequenceService seq;
		
	/**
	 * This implementation always returns {@code true}.
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String tradeid=seq.getId();
		MDC.put("tradeid", tradeid);
		MDC.put("sessionid", request.getSession().getId());
	

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			@Nullable ModelAndView modelAndView) throws Exception {
	}
	
	/**
	 * This implementation is empty.
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
			@Nullable Exception ex) throws Exception {
		MDC.remove("tradeid");
		MDC.remove("sessionid");
	}
	

}
